Total Complexity | 4 |
Total Lines | 33 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import {Inject} from '@nestjs/common'; |
||
9 | |||
10 | export class HolidayToEventsConverter { |
||
11 | constructor( |
||
12 | @Inject('IEventRepository') |
||
13 | private readonly eventRepository: IEventRepository, |
||
14 | @Inject('IDateUtils') |
||
15 | private readonly dateUtils: IDateUtils |
||
16 | ) {} |
||
17 | |||
18 | public convert(holiday: Holiday): void { |
||
19 | const user = holiday.getUser(); |
||
20 | const type = |
||
21 | holiday.getLeaveType() === HolidayLeaveType.MEDICAL |
||
22 | ? EventType.MEDICAL_LEAVE |
||
23 | : EventType.HOLIDAY; |
||
24 | |||
25 | const dates = this.dateUtils.getWorkedDaysDuringAPeriod( |
||
26 | new Date(holiday.getStartDate()), |
||
27 | new Date(holiday.getEndDate()) |
||
28 | ); |
||
29 | |||
30 | const firstDate = dates[0].toISOString(); |
||
31 | const lastDate = dates[dates.length - 1].toISOString(); |
||
32 | |||
33 | for (const date of dates) { |
||
34 | const currentDate = date.toISOString(); |
||
35 | const time = |
||
36 | (firstDate === currentDate && false === holiday.isStartsAllDay()) || |
||
37 | (lastDate === currentDate && false === holiday.isEndsAllDay()) |
||
38 | ? 50 |
||
39 | : 100; |
||
40 | |||
41 | this.eventRepository.save(new Event(type, user, time, currentDate)); |
||
42 | } |
||
45 |